<?php
class BotDetector {
	//detectBot("msnbot", "207.68.124.0");
	//var_dump(verifyGoogleIp("113.233.160.0"));
	private function abc(){
		echo '12345';die;
	}
	public static function detectBot($agent, $ip) {
		//$agent = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
		//$ip = '66.249.69.91';
		$ip = '__'.$ip;
		//var_dump(BotDetector::verifyGoogleBot($agent, $ip)) ;
		
		//var_dump(BotDetector::verifyMsnBot($agent, $ip));
		//var_dump(BotDetector::verifyYahooBot($agent, $ip)) ;

		if(BotDetector::verifyGoogleBot($agent, $ip)) return true;
		else if(BotDetector::verifyMsnBot($agent, $ip)) return true;
		else if(BotDetector::verifyYahooBot($agent, $ip)) return true;
		return false;
	}
	public static function getRealIpAddr()
	{

	    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
	    {
	      $ip=$_SERVER['HTTP_CLIENT_IP'];
	    }
	    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
	    {
	      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
	    }
	    else
	    {
	      $ip=$_SERVER['REMOTE_ADDR'];
	    }
	    
	    return $ip;
	}
	public static function verifyGoogleBot($agent, $ip) {
		$pos = strpos($agent, "Googlebot");
		
		if($pos >= 0) $flag = true;
		if(is_bool($pos)) {
			if(!$pos) $flag = false;
		}
		if($flag) {
			return BotDetector::verifyGoogleIp($ip);
		}
		return false;
	}

	public static function verifyMsnBot($agent, $ip) {
		$pos = strpos($agent, "msnbot");
		if($pos >= 0) $flag = true;
		if(is_bool($pos)) {
			if(!$pos) $flag = false;
		}
		if($flag) {
			return BotDetector::verifyMsnIp($ip);
		}
		return false;
	}
	
	public static function verifyYahooBot($agent, $ip) {
		$pos = strpos($agent, "yahoo");
		if($pos >= 0) $flag = true;
		if(is_bool($pos)) {
			if(!$pos) $flag = false;
		}
		if($flag) {
			return BotDetector::verifyYahooIp($ip);
		}
		return false;
	}
	
	//var_dump(detectBot("msnbot",""));	
	
	public static function verifyGoogleIp($ip) {

		if(strpos($ip, "64.233.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("64.233."));
			$sub_ip = explode(".", $str);
			if(160 <= $sub_ip[0] && $sub_ip[0] <= 191) return true ;
			else return false;
		}		
		if(strpos($ip, "66.102.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("66.102.") );
			$sub_ip = explode(".", $str);
			if(0 <= $sub_ip[0] && $sub_ip[0] <= 15) return true ;
			else return false;
		}
		if(strpos($ip, "66.249.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("66.249."));
			$sub_ip = explode(".", $str);
			if(64 <= $sub_ip[0] && $sub_ip[0] <= 95) return true ;
			else return false;
		}	
		if(strpos($ip, "72.14.")== 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("72.14."));
			$sub_ip = explode(".", $str);
			if(192 <= $sub_ip[0] && $sub_ip[0] <= 255) return true ;
			else return false;
		}	
		if(strpos($ip, "74.125.")== 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("74.125."));
			$sub_ip = explode(".", $str);
			if(0 <= $sub_ip[0] && $sub_ip[0] <= 255) return true ;
			else return false;
		}		
		if(strpos($ip, "209.85.")== 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("209.85."));
			$sub_ip = explode(".", $str);
			if(128 <= $sub_ip[0] && $sub_ip[0] <= 255) return true ;
			else return false;
		}	
		if(strpos($ip, "216.239.")== 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("216.239."));
			$sub_ip = explode(".", $str);
			if(32 <= $sub_ip[0] && $sub_ip[0] <= 63) return true ;
			else return false;
		}
		return false ;
	}
	
	public static function verifyMsnIp($ip) {
		if(strpos($ip, "64.4.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("64.4."));
			$sub_ip = explode(".", $str);
			if(0 <= $sub_ip[0] && $sub_ip[0] <= 63) return true ;
			else return false;
		} 
		if(strpos($ip, "65.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("65."));
			$sub_ip = explode(".", $str);
			if(52 <= $sub_ip[0] && $sub_ip[0] <= 55) return true ;
			else return false;
		} 
		if(strpos($ip, "207.46.") == 2) {
			
			return true ;
		} 
		if(strpos($ip, "207.68.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("207.68."));
			$sub_ip = explode(".", $str);
			if(128 <= $sub_ip[0] && $sub_ip[0] <= 207) return true ;
			else return false;
		}
		return false;
	}
	
	public static function verifyYahooIp($ip) {
		if(strpos($ip, "8.12.144.") == 2) {
			return true; 
		}
		if(strpos($ip, "66.196.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("66.196."));
			$sub_ip = explode(".", $str);
			if(64 <= $sub_ip[0] && $sub_ip[0] <= 127) return true ;
			else return false;
		}
		if(strpos($ip, "66.228.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("66.228."));
			$sub_ip = explode(".", $str);
			if(160 <= $sub_ip[0] && $sub_ip[0] <= 191) return true ;
			else return false;
		}
		if(strpos($ip, "67.195.") == 2) {
			return true;
		}
		if(strpos($ip, "68.142.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("68.142."));
			$sub_ip = explode(".", $str);
			if(192 <= $sub_ip[0] && $sub_ip[0] <= 255) return true ;
			else return false;
		}
		if(strpos($ip, "72.30.") == 2) {
			return true;
		}
		if(strpos($ip, "74.6.") == 2) {
			return true;
		}
		if(strpos($ip, "202.160.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("202.160."));
			$sub_ip = explode(".", $str);
			if(176 <= $sub_ip[0] && $sub_ip[0] <= 191) return true ;
			else return false;
		}
		if(strpos($ip, "209.191.") == 2) {
			$ip = str_replace('__','',$ip);
			$str = substr($ip, strlen("209.191."));
			$sub_ip = explode(".", $str);
			if(64 <= $sub_ip[0] && $sub_ip[0] <= 127) return true ;
			else return false;
		}
		return false;
	}
}
	
	//var_dump(verifyGoogleIp("64.233.191.255"));
	//var_dump(verifyGoogleIp("66.102.15.255"));
	//var_dump(verifyGoogleIp("66.249.95.255"));
	//var_dump(verifyGoogleIp("72.14.255.255"));
	//var_dump(verifyGoogleIp("74.125.255.255"));
	//var_dump(verifyGoogleIp("209.85.255.255"));
	//var_dump(verifyGoogleIp("216.239.63.255"));
	
	//var_dump(verifyMsnIp("64.4.63.255"));
	//var_dump(verifyMsnIp("65.55.255.255"));
	//var_dump(verifyMsnIp("207.46.255.255"));
	//var_dump(verifyMsnIp("207.68.207.255"));
	
	//var_dump(verifyYahooIp("8.12.144.255"));
	//var_dump(verifyYahooIp("66.196.127.255"));
	//var_dump(verifyYahooIp("66.228.191.255"));
	//var_dump(verifyYahooIp("67.195.255.255"));
	//var_dump(verifyYahooIp("68.142.255.255"));
	//var_dump(verifyYahooIp("72.30.255.255"));
	//var_dump(verifyYahooIp("74.6.255.255"));
	//var_dump(verifyYahooIp("202.160.191.255"));
	//var_dump(verifyYahooIp("209.191.127.255"));
	//detect("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", "test");
?>